<?php

namespace app\admin\controller;

use app\admin\model\AdminModel;
use think\Request;
use think\Validate;
use app\admin\model\AdminRoleModel;

class Admin extends Base
{
    /**
     * 管理员列表
     */
    public function index(Request $request)
    {
        $username = $request->param('username');
        $admin_model = new AdminModel;
        $all_user = $admin_model->getAllUser($username);
        //角色名称
        $admin_role_model = new AdminRoleModel;
        $role = $admin_role_model->getAllRole();
        return $this->fetch('', [
            'data' => $all_user,
            'username' => $username,
            'role' => $role,
        ]);
    }

    /**
     * 添加用户
     * @param Request $request
     * @return mixed|\think\response\Json
     */
    public function adminAdd(Request $request)
    {
        if ($request->isAjax()) {
            $validate = new Validate([
                'username|用户名' => 'require|length:4,18|token',
                'password|密码' => 'require|length:6,18',
                'nickname|姓名' => 'length:0,10',
                'phone|手机号' => 'number|length:11',
                'email|邮箱' => 'email',
                'role_id|角色' => 'require|number',
                'remark|备注' => 'length:0,100',
            ]);
            if ($validate->check($request->param())) {
                $admin_model = new AdminModel;
                $res = $admin_model->adminAdd($request->param());
                if ($res['status'] == 'n') {
                    $res['token'] = $request->token();
                }
                return json($res);
            } else {
                return json(['status' => 'n', 'info' => $validate->getError(), 'token' => $request->token()]);
            }
        }
        $admin_role_model = new AdminRoleModel;
        $role = $admin_role_model->getAllRole();
        return $this->fetch('', [
            'role' => $role
        ]);
    }

    /**
     * 修改用户
     * @param Request $request
     * @return mixed|\think\response\Json
     */
    public function adminEdit(Request $request)
    {
        $admin_model = new AdminModel;
        if ($request->isAjax()) {
            $validate = new Validate([
                'username|用户名' => 'require|length:4,18|token',
                'password|密码' => 'length:6,18',
                'nickname|姓名' => 'length:0,10',
                'phone|手机号' => 'number|length:11',
                'email|邮箱' => 'email',
                'role_id|角色' => 'require|number',
                'remark|备注' => 'length:0,100',
            ]);
            if ($validate->check($request->param())) {
                $res = $admin_model->adminEdit($request->param());
                if ($res['status'] == 'n') {
                    $res['token'] = $request->token();
                }
                return json($res);
            } else {
                return json(['status' => 'n', 'info' => $validate->getError(), 'token' => $request->token()]);
            }
        }
        //修改需要的数据
        $data = $admin_model->getUser($request->param('id'));
        $admin_role_model = new AdminRoleModel;
        $role = $admin_role_model->getAllRole();
        if (empty($data)) {
            $this->error('用户不存在', url('admin/admin/index'));
        }
        return $this->fetch('', ['data' => $data, 'role' => $role]);
    }

    /**
     * 禁止用户登录
     */
    public function banUser(Request $request)
    {
        $admin_model = new AdminModel;
        //获取用户数据
        $user = $admin_model->getUser($request->param('id'));
        if (empty($user)) {
            return json(['status' => 'n', 'info' => '用户不存在']);
        }
        if ($user['status'] == 1) {
            $edit_data = ['status' => 0];
        } else {
            $edit_data = ['status' => 1];
        }
        $id = $request->param('id');

        //超级用户不允许被修改
        if ($id == 1) {
            return json(['status' => 'n', 'info' => '超级管理员不允许被此操作']);
        }

        $res = $admin_model->where('id', $id)->update($edit_data);
        if ($res !== false) {
            return json(['status' => 'y', 'info' => '禁用成功！']);
        } else {
            return json(['status' => 'n', 'info' => '禁用失败！']);
        }
    }

    /**
     * 删除用户
     */
    public function delUSer(Request $request)
    {
        $admin_model = new AdminModel;
        //获取用户数据
        $user = $admin_model->getUser($request->param('id'));
        if (empty($user)) {
            return json(['status' => 'n', 'info' => '用户不存在']);
        }
        $id = $request->param('id');
        //超级用户不允许被修改
        if ($id == 1) {
            return json(['status' => 'n', 'info' => '超级管理员不允许被此操作']);
        }
        $res = $admin_model->where('id', $id)->delete();
        if ($res) {
            return json(['status' => 'y', 'info' => '删除成功！']);
        } else {
            return json(['status' => 'n', 'info' => '删除失败！']);
        }
    }

}
